package com.eas.ereceipt.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageButton;
import android.widget.Spinner;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.eas.ereceipt.R;
import com.eas.ereceipt.adapter.DateAdapter;
import com.eas.ereceipt.adapter.ReceiptPagerAdapter;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class ReceiptCollectionFragment extends SherlockFragment implements OnClickListener {
	
	private ViewPager receiptPager;
	private ReceiptPagerAdapter receiptPagerAdapter;
	private DateAdapter dateAdapter;
	private ImageButton btnNewReceipt;
	private Spinner spDate;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View rootView = inflater.inflate(R.layout.fragment_receipt_collection, container, false);
		
		receiptPager = (ViewPager) rootView.findViewById(R.id.pager);
		receiptPagerAdapter = new ReceiptPagerAdapter(getFragmentManager());
		receiptPager.setAdapter(receiptPagerAdapter);
		
		return rootView;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setUpActionBar();
	}
	
	private void setUpActionBar(){
		
		SlidingFragmentActivity parrent = (SlidingFragmentActivity) getActivity();
		ActionBar bar = parrent.getSupportActionBar();
		View v = parrent.getLayoutInflater().inflate(R.layout.ab_receipt_collection, null);
		bar.setCustomView(v);
		
		spDate = (Spinner) v.findViewById(R.id.spDate);
		btnNewReceipt= (ImageButton) v.findViewById(R.id.btnNewReceipt);
		
		dateAdapter = new DateAdapter(parrent);
		spDate.setAdapter(dateAdapter);
		
		spDate.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> a, View v,
					int pos, long id) {
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> a) {
				
			}
		});
		btnNewReceipt.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnNewReceipt:
			startActivity(new Intent(getActivity(), NewReceiptActivity.class));
			break;

		default:
			break;
		}
	}

}
